﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Core
{
    /// <summary>
    /// Expression
    /// </summary>
    public class LogicalExpression
    {
        /// <summary>
        /// Left operand
        /// </summary>
        protected String _LeftOperand;

        /// <summary>
        /// Operator
        /// </summary>
        protected String _Operator;

        /// <summary>
        /// Right operand
        /// </summary>
        protected String _RightOperand;

        /// <summary>
        /// Constructor
        /// </summary>
        public LogicalExpression(String input)
        {

            // ?<left>[A-Z0-9]+            : left operand (group name: left)
            // \s*                         : whitespace or no whitespace
            // ?<operator>(>=|<=|<>|>|<|=) : operator (group name: operator - must be in order !!!)
            // \s*                         : whitespace or no whitespace
            // ?<right>[A-Z0-9]+           : right operand (group name: right)
            String pattern = @"(?<left>[A-Za-z0-9]+)
                               (?<whitespace>\s*)
                               (?<operator>(>=|<=|<>|>|<|=))
                               (?<whitespace>\s*)
                               (?<right>[A-Za-z0-9]+)";

            if(Regex.IsMatch(input, pattern, RegexOptions.IgnorePatternWhitespace))
            {
                Match match = Regex.Match(input, pattern, RegexOptions.IgnorePatternWhitespace);
                
                _LeftOperand = match.Groups["left"].Value;   // left operand
                _Operator = match.Groups["operator"].Value;  // operator   
                _RightOperand = match.Groups["right"].Value; // right operand
            }
            else
            {
                throw new Exception("Expression ERROR!");
            }
        }

        /// <summary>
        /// Get result
        /// </summary>
        /// <returns></returns>
        public Boolean GetResult()
        {
            switch (_Operator)
            {
                case ">":
                    return Double.Parse(_LeftOperand) > Double.Parse(_RightOperand);
                case "<":
                    return Double.Parse(_LeftOperand) < Double.Parse(_RightOperand);
                case "=":
                    return Double.Parse(_LeftOperand) == Double.Parse(_RightOperand);
                case ">=":
                    return Double.Parse(_LeftOperand) >= Double.Parse(_RightOperand);
                case "<=":
                    return Double.Parse(_LeftOperand) <= Double.Parse(_RightOperand);
                case "<>":
                    return Double.Parse(_LeftOperand) != Double.Parse(_RightOperand);
            }

            throw new NotSupportedException();
        }

        /// <summary>
        /// Override ToString method
        /// </summary>
        /// <returns></returns>
        public override String ToString()
        {
            return String.Format("{0} {1} {2}", _LeftOperand, _Operator, _RightOperand);
        }
    }
}
